package com.lsy.controller;

import com.lsy.dao.*;
import com.lsy.entity.*;
import com.lsy.service.AccountListService;
import com.lsy.service.ArticleListService;
import com.lsy.service.CommentListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 项目名：springboot01
 * 包名：com.lsy.controller
 * 类名：ArticleController
 * 描述：
 * 作者：柳胜酉
 * 开始时间：2023/6/13
 * 完成时间：
 * 版本：1.0
 */
@Controller
public class ArticleController {
    @Autowired
    private ArticleListService articleListService;
    @Autowired
    private CommentListService commentListService;
    @Autowired
    private AccountListService accountListService;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private AccountAndArticleCndMapper accountAndArticleCndMapper;
    @Autowired
    private AccountAndCommentCndMapper accountAndCommentCndMapper;
    @Autowired
    private AccountAndArticleAndCommentMapper accountAndArticleAndCommentMapper;
    @Autowired
    private ArticleTypeMapper articleTypeMapper;

    @RequestMapping("/index")
    public ModelAndView index() {
        List<AccountAndArticleCnd> accountAndArticleCnds = accountAndArticleCndMapper.selectAccountAndArticleCnd();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("accountAndArticleCnds", accountAndArticleCnds);
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping({"/articleList"})
    public ModelAndView articleList() {
        List<AccountAndArticleCnd> accountAndArticleCnds = accountAndArticleCndMapper.selectAccountAndArticleCnd();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("accountAndArticleCnds", accountAndArticleCnds);
        modelAndView.setViewName("main");
        return modelAndView;
    }

    @RequestMapping("/backstage")
    public ModelAndView backstage(Authentication authentication,HttpSession session) {
        User user = (User) authentication.getPrincipal();
        Account account = accountMapper.selectUserByName(user.getUsername());
        session.setAttribute("user_id",account.getUser_id());
        List<AccountAndArticleCnd> accountAndArticleCnds = accountAndArticleCndMapper.selectAccountAndArticleCndByUserId(account.getUser_id());
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("accountAndArticleCnds", accountAndArticleCnds);
        modelAndView.setViewName("mymain");
        return modelAndView;
    }

    @RequestMapping("/approved")
    public ModelAndView approved() {
        List<AccountAndArticleCnd> accountAndArticleCnds = accountAndArticleCndMapper.selectAccountAndArticleCnd();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("accountAndArticleCnds", accountAndArticleCnds);
        modelAndView.setViewName("reviewarticles");
        return modelAndView;
    }

    @RequestMapping("/review")
    public ModelAndView review() {
        List<AccountAndArticleCnd> accountAndArticleCnds = accountAndArticleCndMapper.selectAccountAndArticleCndReview();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("accountAndArticleCnds", accountAndArticleCnds);
        modelAndView.setViewName("reviewarticles");
        return modelAndView;
    }

    @RequestMapping("/failed")
    public ModelAndView failed() {
        List<AccountAndArticleCnd> accountAndArticleCnds = accountAndArticleCndMapper.selectAccountAndArticleCndFailed();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("accountAndArticleCnds", accountAndArticleCnds);
        modelAndView.setViewName("reviewarticles");
        return modelAndView;
    }

    @RequestMapping("/commentList")
    public ModelAndView commentList(HttpSession session) {
        Integer user_id = (int) session.getAttribute("user_id");
        List<AccountAndArticleAndCommentCnd> accountAndArticleAndCommentCnds = accountAndArticleAndCommentMapper.selectAccountAndArticleAndCommentCndByUserId(user_id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("accountAndArticleAndCommentCnds", accountAndArticleAndCommentCnds);
        modelAndView.setViewName("/mycomments");
        return modelAndView;
    }

    @RequestMapping("/commentListAll")
    public ModelAndView commentListAll() {
        List<AccountAndCommentCnd> accountAndCommentCnds = accountAndCommentCndMapper.selectAccountAndCommentCnd();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("accountAndCommentCnds", accountAndCommentCnds);
        modelAndView.setViewName("/commentsall");

        return modelAndView;
    }

    @RequestMapping("/deleteComment")
    public String deleteComment(Integer comment_id) {
        commentMapper.deleteCommentByComment_id(comment_id);
        return "redirect:/commentList";
    }

    @RequestMapping("/deleteArticle")
    public String deleteArticle(Integer article_id) {
        articleMapper.deleteArticleByArticle_id(article_id);
        return "redirect:/backstage";
    }

    @RequestMapping("/deleteArticleType")
    public String deleteArticleType(Integer type_id) {
        articleTypeMapper.deleteById(type_id);
        return "redirect:/typeList";
    }

    @RequestMapping("/updateArticle")
    public ModelAndView updateArticle(Integer article_id) {
        Article article = articleMapper.selectArticleByArticleId(article_id);
        List<ArticleType> articleTypes = articleTypeMapper.selectList(null);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("articleTypes", articleTypes);
        modelAndView.addObject("article", article);
        modelAndView.setViewName("update_article");
        return modelAndView;
    }

    @RequestMapping("/doUpdateArticle")
    public String doUpdateArticle(Article article) {
        articleMapper.updateArticleByArticle_id(article);
        return "redirect:/backstage";
    }

    @RequestMapping("/approvedArticle")
    public String approvedArticle(Article article) {
        articleMapper.updateArticleToApproved(article);
        return "redirect:/approved";
    }

    @RequestMapping("/failedArticle")
    public String failedArticle(Article article) {
        articleMapper.updateArticleToFailed(article);
        return "redirect:/failed";
    }

    @RequestMapping("/add_type")
    public String add_type() {
        return "add_articletype";
    }

    @RequestMapping("/typeList")
    public ModelAndView typeList() {
        List<ArticleType> articleType = articleTypeMapper.selectList(null);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("articleType", articleType);
        modelAndView.setViewName("/articletype");

        return modelAndView;
    }


    @RequestMapping("/addArticleType")
    public String addArticleType(String type_name) {
        System.out.println(type_name);
        articleTypeMapper.addArticleType(type_name);
        return "redirect:/typeList";
    }


    @RequestMapping("/articleDetails")
    public String articleDetails(Model model, Integer article_id) {
        Article article = articleListService.getById(article_id);
        Account account = accountListService.getById(article.getUser_id());
        AccountAndArticleCnd accountAndArticleCnd = new AccountAndArticleCnd();
        accountAndArticleCnd.setArticle_id(article_id);
        accountAndArticleCnd.setArticle_title(article.getArticle_title());
        accountAndArticleCnd.setArticle_content(article.getArticle_content());
        accountAndArticleCnd.setType_name(article.getType_name());
        accountAndArticleCnd.setUpload_time(article.getUpload_time());
        accountAndArticleCnd.setUser_id(article.getUser_id());
        accountAndArticleCnd.setArticle_likes(article.getArticle_likes() + 1);
        accountAndArticleCnd.setState(article.getState());
        accountAndArticleCnd.setAccount(account.getAccount());
        accountAndArticleCnd.setPassword(account.getPassword());
        accountAndArticleCnd.setState(account.getRole());
        System.out.println(accountAndArticleCnd.getArticle_likes());
        articleMapper.updateArticleViews(accountAndArticleCnd);

        List<AccountAndCommentCnd> comments = accountAndCommentCndMapper.selectAccountAndCommentCndByArticleId(article_id);
        model.addAttribute("accountAndArticleCnd", accountAndArticleCnd);
        model.addAttribute("comments", comments);

        return "articledetails";
    }

    /**
     * 转到添加文章页
     *
     * @return
     */
    @RequestMapping("/add_article")
    public ModelAndView add_article() {
        List<ArticleType> articleTypes = articleTypeMapper.selectList(null);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("articleTypes", articleTypes);
        modelAndView.setViewName("/add_article");
        return modelAndView;
    }

    /**
     * 添加文章
     *
     * @param article
     * @param request
     * @return
     */
    @RequestMapping("/addArticle")
    public ModelAndView addArticle(Article article, HttpServletRequest request) throws ParseException {
        ModelAndView modelAndView = new ModelAndView();
        HttpSession session = request.getSession();
        Integer user_id = (Integer) session.getAttribute("user_id");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = df.format(new Date());
        Date uploadTime = df.parse(time);
        article.setUser_id(user_id);
        article.setArticle_title(article.getArticle_title());
        article.setArticle_content(article.getArticle_content());
        article.setType_name(article.getType_name());
        article.setUpload_time(uploadTime);
        article.setArticle_likes(0);
        article.setState(0);
        articleMapper.insert(article);
        modelAndView.setViewName("redirect:/backstage");
        return modelAndView;
    }

    @RequestMapping("/addComment")
    public ModelAndView addComment(AccountAndArticleCnd accountAndArticleCnd,Comment comment, HttpServletRequest request) throws ParseException {
        ModelAndView modelAndView = new ModelAndView();
        HttpSession session = request.getSession();
        Integer user_id = (Integer) session.getAttribute("user_id");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = df.format(new Date());
        Date uploadTime = df.parse(time);
        comment.setUser_id(user_id);
        comment.setComment_time(uploadTime);
        comment.setArticle_id(accountAndArticleCnd.getArticle_id());
        commentMapper.addComments(comment);
        modelAndView.setViewName("redirect:/backstage");
        return modelAndView;
    }
}
